<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Base class containing common methods for all descendents of the JFX_Form class
 *
 * @author Anthony Gallon
 * @package  jfx-core-classes
 */




/**
 * Base class containing common methods for all descendents of the JFX_Form class
 *
 * @author Anthony Gallon
 * @package  jfx-core-classes
 */

class JFX_Form_Component
{
    /**
     * Defines the classnames for the element
     * @var array $classnames
     */
    protected $classnames = array();
    /**
     * Defines the HTML id for the element
     * @var string $id
     */
    protected $id = '';
    /**
     * Defines whether the element is XHTML compliant
     * @var bool $isXhtml
     */
    protected $isXhtml = true;
    /**
     * Contains all parameters (HTML attributes) as key=>value pairs
     * @var array $parameters
     */
    protected $parameters = array();
    /**
     * Defines whether the element is marked as being a required field
     * @var bool $required
     */
    protected $required = false;
    /**
     * Defines whether the element is marked as HTML disabled
     * @var bool $disabled
     */
    protected $disabled = false;
    /**
     * Defines whether to show the label element for the corresponding input
     * @var bool $showLabel
     */
    protected $showLabel = true;
    /**
     * Defines whether to wrap the element in a div tag
     * @var bool $wrap
     */
    protected $wrap = true;
    /**
     * Defines the HTML name attribute
     * @var string $name
     */
    protected $name = '';
    /**
     * Defines the HTML value of the element
     * @var string $value
     */
    protected $value = '';
    /**
     * Defines the type of element to render
     * @var string $type
     */
    protected $type = NULL;

    /**
     * Reference to the form element which this element belongs to, if any
     * @var object JFX_Form
     */
    public $formObj = NULL;

    /**
     * Reference to the block element which this element belongs to, if any
     * @var object JFX_Form_Block
     */
    public $blockObj = NULL;

    /**
     * Reference to the fieldset object which this element belongs to, if any
     * @var object JFX_Form_Fieldset
     */
    public $fieldsetObj = NULL;

    /**
     * Label element contains this text to describe the form field
     * @var string $label
     */
    public $label = '';

    public function __toString(){
        return $this->render();
    }

    /**
     * Set whether the element is marked as a required field
     * @param bool $bool
     * @return object JFX_Form_Component
     */
    public function required($bool=''){
        if($bool===true || $bool===false) $this->required = $bool;
        return $this;
    }

    /**
     * Get the name of the element
     * @return string $name
     */
    public function getName(){
      return $this->name;
    }

    /**
     * Set the HTML id of the element
     * @param string $id
     * @return object JFX_Form_Component
     */
    public function setId($id=''){
        if($id=='')$id = 'JFX_Form_Component'.rand();
        $this->id = $id;
        return $this;
    }

    /**
     * Add some text to go after the form field when rendering
     * @param string $text
     * @return object JFX_Form_Component
     */
    public function addAftertext($text){
        $this->aftertext .= $text;
        return $this;
    }

    /**
     * Set whether this form field is wrapped in a div
     * @param bool $bool
     * @return object JFX_Form_Component
     */
    public function setWrap($bool){
        $this->wrap = (bool) $bool;
        return $this;
    }

    /**
     * Set whether to show the label element for the corresponding form field
     * @param bool $bool
     * @return object JFX_Form_Component
     */
    public function showLabel($bool){
        $this->showLabel = (bool) $bool;
        return $this;
    }

    /**
     * Get the HTML id
     * @return string $id
     */
    public function getId(){
        return $this->id;
    }

    /**
     * Add a new HTML attribute
     * @param string $name
     * @param string $value
     * @return object JFX_Form_Component
     */
    public function addParameter($name, $value){
        $this->parameters[] = array('name' => $name, 'value' => $value);
        return $this;
    }

    /**
     * Add a new HTML attribute
     * @param string $name
     * @param string $value
     * @return object JFX_Form_Component
     */
    public function addAttribute($name, $value){
        $this->addParameter($name, $value);
        return $this;
    }

    /**
     * Add a new HTML attribute
     * @param string $name
     * @param string $value
     * @return object JFX_Form_Component
     */
    public function addJsEvent($action, $function){
        return $this->addParameter($action, $function);
    }

    /**
     * Get the assigned attributes as a string
     * @return string $htmlAttributes
     */
    public function renderAttributes(){
        $returnString = '';
        foreach($this->parameters as $event){
            $returnString .= ' '.$event['name'].'="'.$event['value'].'" ';
        };
        return $returnString;
    }

    /**
     * Get the assigned attributes as a string
     * @return string $htmlAttributes
     */
    public function renderParameters(){
      return $this->renderAttributes();
    }

    /**
     * Tell this element if it is to render in XHTML compliant mode
     * @param bool $param
     * @return bool $param
     */
    public function isXhtml($param = ''){
        if($param === true || $param === false) $this->isXhtml = $param;
        return $this->isXhtml;
    }

    /**
     * Add a new classname to the element
     * @param string $classname
     * @return object JFX_Form_Component
     */
    public function addClass($classname){
        $this->classnames[] = $classname;
        return $this;
    }

    /**
     * Get the classnames as a space separated string
     * @return string $classNames
     */
    public function renderClasses(){
        if(count($this->classnames)==0) return '';
        return ' class="'.implode(' ', $this->classnames).'" ';
    }

    /**
     * Tell this element if it is to render as disabled
     * @param bool $bool
     * @return object JFX_Form_Component
     */
    public function disabled($bool){
        if($bool === true || $bool === false) $this->disabled = $bool;
        return $this;
    }

    /**
     * Return the provided string wrapped in a div with provided class name
     * @param string $string
     * @param string $classname
     * @return string $string
     */
    protected function wrap($string, $classname = 'form-field'){
        if($this->type == 'hidden') return $string;
        $isError = (JFX::isFormError($this->name)) ? ' error' : '';
        $this->addClass($classname.$isError.' '.$this->type);
        return '<div '.$this->renderClasses().' id="'.$this->id.'_component">'.$string.'</div>';
    }

}
